//
//  ViewController.swift
//  swift3.0Exercise
//
//  Created by 刘培壮 on 16/9/18.
//  Copyright © 2016年 com.solidInfo. All rights reserved.
//

import UIKit
import ObjectMapper

fileprivate let screenWidth = UIScreen.main.bounds.width
fileprivate let screenHeight = UIScreen.main.bounds.height
fileprivate let cellIdentifier = "cellIdentifier"

class CityViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    private lazy var tableView: UITableView = {
        $0.dataSource  = self
        $0.delegate = self
        $0.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
        return $0
    }(UITableView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight), style: .plain))
    
    private lazy var dataSource: (citymodels: [[CityModel]],cityFirsts: [String]) = {
        let path = Bundle.main.path(forResource: "citysPinyin", ofType: "json")
        let data = try! Data(contentsOf: URL(fileURLWithPath: path!), options: .alwaysMapped)
        let dictionary = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
        let model = Mapper<CitysModel>().map(JSONObject: dictionary)! as CitysModel
        let citys = model.citys!
        var sortedCitys = citys.sorted(by: {
            let result = $0.newPinyin.compare($1.newPinyin, options: .numeric)
            return result == .orderedAscending
        })
        var array = [] as [[CityModel]]
        var letters = [] as [String]
        
        for item in sortedCitys {
            if !letters.contains(item.firstLetter!) {
                letters.append(item.firstLetter!)
            }
        }
       
        letters.forEach {
            print($0)
        }
        
        for letter in letters {
            var subArr = [] as [CityModel]
            for item in sortedCitys {
                if item.firstLetter == letter {
                    subArr.append(item)
                }
            }
            array.append(subArr)
        }
        
        letters.insert("国内", at: 0)
        letters.insert("历史", at: 0)
        array.insert([], at: 0)
        array.insert([], at: 0)
        return (array,letters)
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        view.addSubview(tableView)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSource.1.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.0[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
        cell.textLabel?.text = dataSource.0[indexPath.section][indexPath.row].name
        return cell
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let title = dataSource.cityFirsts[section]
        if title == "国内" {
            return "国内热门城市"
        } else if title == "历史" {
            return "历史访问城市"
        } else {
            return dataSource.1[section]
        }
    }
    
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return dataSource.1
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = dataSource.citymodels[indexPath.section][indexPath.row]
        dataSource.citymodels.remove(at: 0)
        dataSource.citymodels.insert([model], at: 0)
        tableView.reloadData()
    }
}

